package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.system.entity.SysUser;
import com.sxkf.system.mapper.SysUserMapper;
import com.sxkf.system.vo.PassStoreAuditVO;
import com.sxkf.template.entity.SettledRecord;
import com.sxkf.template.entity.Store;
import com.sxkf.template.entity.User;
import com.sxkf.template.mapper.StoreMapper;
import com.sxkf.template.service.SettledRecordService;
import com.sxkf.template.service.UserService;
import com.sxkf.template.vo.response.SettledUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.CheckEmptyUtil;
import org.jeecg.common.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-05 16:15:45
 */
@Api(tags = "申请开店")
@RestController
@RequestMapping("/settledRecord")
public class SettledRecordController {
    @Resource
    private SettledRecordService settledRecordService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private StoreMapper storeMapper;

    @ApiOperation(value = "查询")
    @GetMapping(value = "/page")
    public R<IPage<SettledRecord>> page(@RequestParam Integer pageNo,@RequestParam Integer pageSize,
                                        @RequestParam(required = false) String type,
                                        @RequestParam(required = false) String state){
        Page<SettledRecord> page = settledRecordService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<SettledRecord>()
                .eq(CheckEmptyUtil.isNotEmpty(type), SettledRecord::getType, type)
                .eq(CheckEmptyUtil.isNotEmpty(state), SettledRecord::getState, state)
                .orderByDesc(SettledRecord::getCreateTime)
        );
        return R.success(page);
    }


    @ApiOperation(value = "申请")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody SettledRecord settledRecord){
        SettledRecord record = settledRecordService.getOne(new LambdaQueryWrapper<SettledRecord>()
                .eq(SettledRecord::getUserId, settledRecord.getUserId())
                .in(SettledRecord::getState, "0","1")
        );
        if (!CheckEmptyUtil.isEmpty(record)){
            return R.error("申请已发送，请耐心等待申请结果。");
        }
        //todo 这里后续要先去进行身份证二要素识别然后再将状态置为1
        settledRecord.setState("1");
        settledRecordService.save(settledRecord);
        return R.success();
    }


    @ApiOperation(value = "通过审核")
    @PostMapping(value = "/pass")
    public R<?> pass(@RequestBody PassStoreAuditVO passStoreAuditVO){
        settledRecordService.pass(passStoreAuditVO);
        return R.success();
    }

    @ApiOperation(value = "驳回审核")
    @PostMapping(value = "/reject")
    public R<?> reject(@RequestParam String id,@RequestParam String reason){
        settledRecordService.reject(id,reason);
        return R.success();
    }

    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    public R<?> delete(@RequestParam String id){
        settledRecordService.removeById(id);
        return R.success();
    }

    @ApiOperation(value = "根据用户id查记录")
    @GetMapping(value = "/getByUserId")
    public R<SettledUserVo> getByUserId(@RequestParam String userId){
        SettledRecord settledRecord = settledRecordService.getOne(new LambdaQueryWrapper<SettledRecord>()
                .eq(SettledRecord::getUserId, userId));

        SettledUserVo settledUserVo = new SettledUserVo();
        if (CheckEmptyUtil.isEmpty(settledRecord)) {
            settledUserVo.setState("-1");
        }else {
            settledUserVo.setState(settledRecord.getState());

            if ("2".equals(settledRecord.getState())){
                SysUser sysUser = sysUserMapper.selectById(settledRecord.getSysUserId());
                settledUserVo.setUsername(sysUser.getUsername());
                String decrypt = PasswordUtil.decrypt(sysUser.getPassword(), sysUser.getUsername(), sysUser.getSalt());
                settledUserVo.setPassword(decrypt);

                Store store = storeMapper.selectById(sysUser.getStoreId());
                settledUserVo.setStore(store);
            }
        }
        return R.success(settledUserVo);
    }

}
